// pages/shop/shop.js
const getToken = require('../../utils/get_token.js');
let [stNum, orderBy,order, busbaseId]=[1,4,'ASC',''];
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getName(that);
  getDeme(that, stNum, orderBy, busbaseId);//初始加载
}
const getDeme = function (that, stNum, orderBy){
  that.setData({
   hidden: false
  })
  let params = [ 'shop/goods/upGoodsByBasebaseId', {"busbaseId":busbaseId,"mark":"all","orderBy": orderBy,"order":order,"startNum": stNum }]
  getToken.getData(function (res) {
    var val = res.data.obj.rows;
    for (var i = 0; i < val.length; i++) {
      var Urls = JSON.parse(val[i].goodsImageStore);
      val[i].img = `https://www.globalhexi.com/${Urls[0].path}/big/${Urls[0].id}.jpg`;
      val[i].pingName=val[i].name;
      val[i].pinCount=val[i].sales;
      val[i].intPinPrice = (val[i].intPrice/100).toFixed(2);
      val[i].intPrice = (val[i].intMarketPrice/100).toFixed(2);
      that.data.ul.push(val[i]);
    }
    that.setData({
      ul: that.data.ul,
      hidden: true
    })
  }, ...params)
};
const getName = function (that) {
  let params = ['assistant','assistant/busbase/bus','{"id":"'+busbaseId+'"}']
  getToken.pay(function (res) {
    console.log(res.data)
    let data=res.data;
    data.buslogo='http://www.globalhexi.com/'+data.buslogo;
    data.types='';
    data.mainProject=JSON.parse(data.mainProject);
    data.mainProject.forEach(function(value,index){
      if(index+1==data.mainProject.length)
      {
        data.types=data.types+value.name;	
      }else{
        data.types=data.types+value.name+'·';
      }
     })
    that.data.s=data;      
    that.setData({
      s: that.data.s
    })
  }, ...params)
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
	currenTab:0,
  s:{},
	ul:[],
  shop:{},
  one:4,
  t:4,
	boolT: true,
  hidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    busbaseId = options.id;
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    ckToken(that);
  },
	changeList:function(e){
    var that=this;
    if(that.data.boolT){
			that.data.ul=[];
      stNum=1;
      let id = Number(e.currentTarget.dataset.id);
			if(id==6)//销量
			{
				if(id==orderBy)//二次点击
				{
          if(order=='DESC')
          {
            order='ASC';
            that.data.one=2;
          }else{
            that.data.one=1;
            order='DESC';  
          }
				}else{
          that.data.one=1;
					order='DESC';  
				}
			}else if(id==4)//价格
			{
				if(id==orderBy)//二次点击
				{
				if(order=='DESC')
				{
					order='ASC';
					that.data.one=4;
				}else{
          that.data.one=3;
					order='DESC';  
				}
				}else{
					that.data.one=3;
					order='DESC';  
				}
		  }else{
         that.data.one=0;
			   order='DESC';  
		  }
			orderBy=id;	
      that.data.t=id;
      that.setData({
        one:that.data.one,
        t:that.data.t
      })
      getDeme(that, stNum, orderBy);
		}
  },
	enter: function (e) {
    var that = this;
    let id=e.currentTarget.dataset.id;
    if(that.data.boolT)
		{
      that.data.boolT = false;
      let params = [ 'shop/goods/goods', {"goodId":id}]
        getToken.getData(function (res) {
          let data=res.data;
          if(data.goodsPinTuanId!='')
					{
            wx.navigateTo({url:'../paired_detail/paired_detail?scene=' + data.goodsPinTuanId,     })
					}else{
            wx.navigateTo({url: '../detail/detail?id=' +id,})
          }	
          setTimeout(function () {that.data.boolT = true;}, 500)
        }, ...params)
		}
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
		var that = this;
		wx.showNavigationBarLoading()//加载动画
		setTimeout(function () {
			wx.hideNavigationBarLoading()//隐藏动画
			wx.stopPullDownRefresh()//停止加载
		}, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var that=this;
    stNum++;
    getDeme(that, stNum, orderBy, busbaseId);
  },

  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function (res) {
		if (res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '和系小程序',
			path: '/pages/shop/shop?id=' + busbaseId,
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			}
		}
	}
})